package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.CustomException;
import com.itheima.entity.Category;
import com.itheima.entity.Dish;
import com.itheima.entity.Setmeal;
import com.itheima.mapper.CategoryMapper;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ClassName: CategoryServiceImpl
 * Package: com.itheima.service.impl
 * Description:
 *
 * @Author: @Albert
 * @Version: 1.0
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService  {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;

    @Override
    public void remove(Long id) {
        log.info("开始删除分类,分类ID为{}",id);
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper();
        dishWrapper.eq(Dish::getCategoryId,id);
        //是否关联菜品
        int count = dishService.count(dishWrapper);
        log.info("得到的关联菜品的值外部{}",count);
        if(count>0){
            log.info("得到的关联菜品的值{}",count);
            throw new CustomException("当前分类关联菜品,无法删除");
        }


        LambdaQueryWrapper<Setmeal> mealWrapper = new LambdaQueryWrapper();
        mealWrapper.eq(Setmeal::getCategoryId,id);
        //是否关联菜品
        count = dishService.count(dishWrapper);
        log.info("得到的关联套餐的值外部{}",count);
        if(count>0){
            log.info("得到的关联套餐的值{}",count);
            throw new CustomException("当前分类关联套餐,无法删除");
        }
        //正常删除
        super.removeById(id);
    }
}
